package imp

import (
	sqlc "bgs/db/sqlc"
	syncGrpc "bgs/grpc/gen/service/sync"
	"bgs/grpc/gen/shared/message"
	"bgs/util"
	"context"
	"database/sql"
)

/****************************************************************************************/
/*                               sync_mapping entity of helper                          */
/****************************************************************************************/

func transformSyncEntityToPb(sync sqlc.Sync) *syncGrpc.SyncEntity {
	return &syncGrpc.SyncEntity{
		Id:           sync.ID,
		LocalObj:     sync.LocalObj,
		LocalPkName:  sync.LocalPkName,
		LocalFields:  sync.LocalFields,
		RemoteObj:    sync.RemoteObj,
		RemotePkName: sync.RemotePkName,
		RemoteFields: sync.RemoteFields,
	}
}

/****************************************************************************************/
/*                               sync entity of cmd                                     */
/****************************************************************************************/

// UpsertSync 插入/更新同步
func (s *SyncAPIService) UpsertSync(ctx context.Context, req *syncGrpc.UpsertSyncRequest) (res *syncGrpc.UpsertSyncResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("syncImp->UpsertSync:%v", req)
	res = &syncGrpc.UpsertSyncResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	arg := sqlc.UpsertSyncParams{
		LocalObj:      req.LocalObj,
		LocalPkName: req.LocalPkName,
		LocalFields:         req.LocalFields,
		RemoteObj:        req.RemoteObj,
		RemotePkName:    req.RemotePkName,
		RemoteFields:     req.RemoteFields,
	}
	sync, err := s.dao.Q.UpsertSync(ctx, arg)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.Sync = transformSyncEntityToPb(sync)
	return
}

/****************************************************************************************/
/*                               sync entity of query                                   */
/****************************************************************************************/

// GetSyncByLocalObjAndLocalPkName 获取单个用户
func (s *SyncAPIService) GetSyncByLocalObjAndLocalPkName(ctx context.Context, req *syncGrpc.GetSyncByLocalObjAndLocalPkNameRequest) (res *syncGrpc.GetSyncByLocalObjAndLocalPkNameResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("syncImp->GetSyncByLocalObjAndLocalPkName:%v", req)
	res = &syncGrpc.GetSyncByLocalObjAndLocalPkNameResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	arg := sqlc.GetSyncByLocalObjAndLocalPkNameParams{
		LocalObj: req.LocalObj,
		LocalPkName: req.LocalPkName,
	}
	sync, sqlErr := s.dao.Q.GetSyncByLocalObjAndLocalPkName(ctx, arg)
	if sqlErr != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    util.Int32IfThenElse(sql.ErrNoRows == sqlErr, 404, 400),
			Msg:     sqlErr.Error(),
		}
		return
	}
	res.Sync = transformSyncEntityToPb(sync)
	return
}